{block #head}
<script type="text/javascript" n:syntax="double">
    var err = false;
    var errMsg = "";
    var index = 0;
    var numOfSteps = 0;
    var updateFunctions = [["{{dictionary update_updateInit}}",{{link updateInit!}}],
                           ["{{dictionary update_backupDatabase}}",{{link backupDatabase!}}],
                           ["{{dictionary update_logToDb}}",{{link logToDb!}}],
                           ["{{dictionary update_backupCurrentVersion}}", {{link backupCurrentVersion!}}],
                           ["{{dictionary update_loadNewFiles}}", {{link loadNewFiles!}}]];                       
    var roolbackFunction = ["{{dictionary update_rollback}}",{{link rollback!}}];
    
    jQuery.extend({
	cms: {
            updateSnippet: function (id, html) {
                var currHtml = $("#" + id).html();
                $("#" + id).html(currHtml + html);
            },                                
            
            error: function () {
                data = { 
                    err: true, 
                    errMsg: "{{dictionary update_serverError}}"
                };
                
                jQuery.cms.success(data);
            },                                
            
            success: function (payload) {
                var msg = "";
                var show = false;
                hideBar();
                
                // v pripade ze nastala chyba
                if (err === false && payload !== null && payload.err) {
                    err = true;
                    msg = "<span class=\"update-fail\">{{dictionary update_operationFail}} (" + payload.errMsg + ")</span><br />";
                    index = numOfSteps;
                }
                
                // v pripade ze operace probehla uspesne
                if (err === false) {
                    msg = "<span class=\"update-succ\">{{dictionary update_operationSucc}}</span><br />";
                }
                
                // dalsi krok
                index++;
                if (err === false && index < numOfSteps) {
                    msg += "<strong>" + updateFunctions[index][0] + ": </strong>";                    
                    $.get(updateFunctions[index][1], jQuery.cms.success)
                        .error(jQuery.cms.error); 
                    show = true;
                }
                
                // proces aktualizace probehl v poradku
                if (err === false && index >= numOfSteps) {
                    msg += "<br /><span class=\"update-succ\">{{dictionary update_updateSucc}}</span>";
                    index = 0;
                    $.get({{link updateFinish!}});
                }
                
                // aktualizace selhala
                if (err && index >= numOfSteps) {
                    msg += "<strong>" + roolbackFunction[0] + ": </strong>";

                    $.get(roolbackFunction[1], function(data) {
                        msg = "<span class=\"update-succ\">{{dictionary update_operationSucc}}</span><br />"
                            + "<br /><span class=\"update-fail\">{{dictionary update_updateFail}}</span>";
                        $("#update-progress").html($("#update-progress").html() + msg);
                    });
                }
                
                $("#update-progress").html($("#update-progress").html() + msg);              
                if (show) {
                    showBar();
                }
            }
	}
    });
    
    $("#update-button").live("click", function (event) {
        event.preventDefault();         
        
        err = false;
        errMsg = "";
        index = 0;
        numOfSteps = updateFunctions.length;
        
        $("#update-progress").html("<br /><strong>" + updateFunctions[index][0] + ": </strong>");   
        showBar();
        $.get(updateFunctions[index][1], jQuery.cms.success)
            .error(jQuery.cms.error);
    });
    
    $(function() {
        $('<div id="ajax-bar"></div>').appendTo("body").hide();
    });
    
    function showBar () {
        var el = $("#update-progress strong").last();
        var offset = el.offset();
        var width = el.width();
        
        $("#ajax-bar").show().css({
            position: "absolute",
            left: offset.left + width + 10,
            top: offset.top
        });
    }
    
    function hideBar () {
        $("#ajax-bar").hide();
    }
</script>
{/block}

{block #content}
<!--<h2>{dictionary update_currentSettings}</h2>-->
<table n:if="isset($config)">
    <tr>
        <td><strong>{dictionary update_svnPath} </strong></td>
        <td>{$config["svnpath"]}</td>
    </tr>
    <tr>
        <td><strong>{dictionary update_svnDir} </strong></td>
        <td>{$config["svndir"]}</td>
    </tr>    
    <tr>
        <td><strong>{dictionary update_bckDir} </strong></td>
        <td>{? echo realpath($appDir . '/' . $config["bckdir"]); }</td>
    </tr>
    <tr>
        <td><strong>{dictionary update_modules} </strong></td>
        <td>{? echo implode(", ", $config["modules"])}</td>
    </tr>
</table>
<br />
<a n:if="isset($config)" href="{link this}" id="update-button" class="a-button">{dictionary update_doUpdate}</a>
<div id="update-progress"></div>


<h2 n:if="isset($config)">{dictionary update_updateHistory}</h2>
    {snippet gridWrapper}
        {control grid}
    {/snippet}
{/block}